<?php

    require_once dirname(dirname(__FILE__)) . '/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';

    /**
     * Description of dao_general
     * @author Viet Anh
     */
    class dao_general
    {
        public function __construct()
        {

        }

        public function check_option($option_name)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT 1 FROM tbl_option WHERE name = '" . $option_name . "' LIMIT 1";
            /** @noinspection PhpParamsInspection */
            $result = mysqli_query($con, $query) or die('Query Failed' . mysqli_error());

            if (mysqli_fetch_row($result)) {
                $db->close_connect();

                return TRUE;
            } else {
                $db->close_connect();

                return FALSE;
            }
        }

        public function set_option_value($option_name, $option_value)
        {
            $db  = new connection();
            $con = $db->open_connect();

            if ($this->check_option($option_name)) {
                $query = "UPDATE tbl_option SET value = '" . $option_value . "' WHERE name = '" . $option_name . "'";
            } else {
                $query = "INSERT INTO tbl_option(name, value) VALUES ('" . $option_name . "', '" . $option_value . "')";
            }
            $result = mysqli_query($con, $query) or die('Failed');

            $db->close_connect();

            return TRUE;
        }

        public function get_option_value($option_name)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT value FROM tbl_option WHERE name = '" . $option_name . "'";
            $result = mysqli_query($con, $query) or die("Failed");
            $row = mysqli_fetch_array($result);

            $db->close_connect();

            return $row['value'];
        }
    }

?>